#!/usr/bin/env pythonw
'''
Created on Mar 15, 2011

@author: mwarren1
'''
import time
import os
from threading import Thread
import wx
from wx.tools.dbg import Logger


from TaskBarIcon import TaskBarIcon
import TutorialGUI
import maildroplibs
    
import StartupSettings

if __name__ == '__main__':
    
    logfile = os.path.join(maildroplibs.SETTINGS_DIR,"log.txt")
    if not os.path.exists(os.path.split(logfile)[0]):
        os.mkdir(os.path.split(logfile)[0])
    app = wx.App(True, filename = logfile)
    
    debug = Logger('main')
    debug.SetStream(maildroplibs.DEBUGSTREAM)
    debug(enable = maildroplibs.ENABLEDEBUG)
    
    ''' Initialize Settings '''
    mySettings = None
        
    debug.dbg('initializing settings')
    mySettings = maildroplibs.MailDropSettings()

    debug.dbg('verifying startup settings')
    StartupSettings.enableStartup(mySettings['SYSTEMSTARTUP'])

    debug.dbg('initializing counter')
    maildroplibs.Counter.Counter()
    
    debug.dbg('forming GUI framework')
    
    
    print
    print "-------- " + time.strftime("%c") + " --------"
    print
    
    #Create System Tray Icon
    debug('initializing system tray icon')
    taskbaricon = TaskBarIcon(mySettings)
    
    if mySettings.firstrun:
        TutorialGUI.TutorialGUI()
        taskbaricon.settingsGUI.Show()

    #Create MailCheck Thread & Start it
    debug.dbg('starting main thread loop')
    Thread(target=maildroplibs.MailThread.MailThread(mySettings).start).start()

    debug.dbg('entering GUI main system loop')
    app.MainLoop()
    
    if maildroplibs.DEBUGSTREAM is not None:
        maildroplibs.DEBUGSTREAM.close()
    
